/*var carselect = new Array();
var storeselect = new Array();
var bmselect = new Array();
var userselect = new Array();
$(function () {
    initThisApp();
})
function initThisApp() {
    // storeselect = [];
    // carselect = [];
    // bmselect = [];
    // userselect = [];
    $.ajax({
        url: "./goPCDD/initDisPage", //请求的url地址
        xhrFields: {
            withCredentials: true
        },
        async:false,
        dataType: "json", //返回格式为json
        type: "GET", //请求方式
        processData: false, //对表单data数据是否进行序列化
        contentType: false, //dataType设置你收到服务器数据的格式
        headers: {
            'Content-Type': 'application/json'
        },
        beforeSend: function() {
            //请求前的处理
        },
        success: function(data) {
            if(data.status==0){
                // top.location.href = "../../login.html";
                window.location.href="./login.html";
            }else{
                var a = JSON.parse(data.data);
                var carnoarr = a.data;
                carselect.splice(0,carselect.length);
                for(var i=0;i<carnoarr.length;i++){
                    var temp = {
                        value: carnoarr[i].CarNo,
                        label: carnoarr[i].CarNo
                    };
                    carselect.push(temp)

                }
                console.log(carselect)
                var storearr = a.data1;
                storearr = storearr.distinct();
                storeselect.splice(0,storeselect.length);
                for(var i=0;i<storearr.length;i++){
                    var temp = {
                        value: storearr[i].Store,
                        label: storearr[i].Store
                    };
                    storeselect.push(temp)
                }
            }
        },
        complete: function() {
            //请求完成的处理
        },
        error: function() {}
    });
}*/
Array.prototype.distinct = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Store.toString() === arr[j].Store.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct1 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Bm.toString() === arr[j].Bm.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct2 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].UserNo.toString() === arr[j].UserNo.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

var pageSize = 99999999;
var vm = new Vue({
    el: "#app",
    data:function(){
        return{
            CarNo:"",
            CarNoList:[],
            SjName:"",
            SjNameList:[],
            StartDate:"",
            EndDate:"",
            //搜索结果
            //车辆返回编辑弹框start
            FanEditModel:false,
            FanModelItem:{
                DocNo:"",
                CarNo:"",
                FanStartKilo:"",
                FanEndKilo:"",
                FanKilometers:"",
                FanRemark:""
            },
            //车辆返回编辑弹框end
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    align: 'center',
                },
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                },
                {
                    title: '约车人',
                    key: 'YueMan',
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'YueStore',
                    align: 'center',
                },
                {
                    title: '约车人部门',
                    key: 'YueBm',
                    align: 'center',
                },
                {
                    title: '司机姓名',
                    key: 'SjName',
                    align: 'center',
                },
                {
                    title: '车牌号',
                    key: 'PaiCarNo',
                    align: 'center',
                },
                {
                    title: '调度类型',
                    key: 'Status',
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="1"){
                            Status = '已约车';
                        }
                        if(Status=="2"){
                            Status = '已派车';
                        }
                        if(Status=="3"){
                            Status = '已返回';
                        }
                        return h('span',Status);
                    }
                },
                {
                    title: '车辆停放位置',
                    key: 'FanPlace',
                    align: 'center',
                },
                {
                    title: '车辆行驶前公里数',
                    key: 'FanStartKilo',
                    align: 'center',
                },
                {
                    title: '车辆行驶后公里数',
                    key: 'FanEndKilo',
                    align: 'center',
                },
                {
                    title: '车辆行驶公里数',
                    key: 'FanKilometers',
                    align: 'center',
                },
                {
                    title: '总花费',
                    key: 'FanCost',
                    align: 'center',
                },
                {
                    title: '备注',
                    key: 'FanRemark',
                    align: 'center',
                },
                {
                    title: '派车时间',
                    key: 'PaiDate',
                    align: 'center',
                },
                {
                    title: '返回时间',
                    key: 'FanDate',
                    align: 'center',
                },
                {
                    title: '操作',
                    key: 'action',
                    width:200,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'ghost',
                                    size: 'small',
                                    icon:'edit',
                                },
                                attrs: {
                                    title: '编辑'
                                },
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '16px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        self.FanEditModel = true;
                                        self.FanModelItem.DocNo=params.row.DocNo;
                                        self.FanModelItem.FanStartKilo=params.row.FanStartKilo;
                                        self.FanModelItem.FanEndKilo=params.row.FanEndKilo;
                                        // self.FanModelItem.FanKilometers=params.row.FanKilometers;
                                        self.FanModelItem.FanRemark=params.row.FanRemark;
                                        self.FanModelItem.CarNo=params.row.PaiCarNo;
                                    }
                                }
                            }),
                        ]);
                    }


                }
            ],
            // 接下来绑定数据，分别对应前面的列的key值来进行数据绑定
            data1: [],
            // 初始化信息总条数
            dataCount:0,
            stripe:true
        }
    },
    created(){
        this.initPage();
        this.queryAllCarNo();
        this.queryAllSjName();
    },
    methods: {
        initPage(){
            var self = this;
            $.ajax({
                url: "./goPCDD/initDisPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="./login.html";
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        queryAllCarNo(){
            var self = this;
            $.ajax({
                url: "./goPCDD/queryAllCarNo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    var carnojson = new Array();
                    var json = JSON.parse(data.data);
                    var tempcarnojson = json.data;
                    var temp = {
                        value: "",
                        label: "全部"
                    };
                    //添加品牌下拉start
                    carnojson.push(temp);
                    for(var i=0;i<tempcarnojson.length;i++){
                        var temp = {
                            value: tempcarnojson[i].CarNo,
                            label: tempcarnojson[i].CarNo
                        };
                        carnojson.push(temp)
                    }
                    self.CarNoList = carnojson;
                },
                error: function() {},
            });
        },
        queryAllSjName(){
            var self = this;
            $.ajax({
                url: "./goPCDD/queryAllSjName", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    var sjnamejson = new Array();
                    var json = JSON.parse(data.data);
                    var tempsjnamejson = json.data;
                    var temp = {
                        value: "",
                        label: "全部"
                    };
                    sjnamejson.push(temp);
                    for(var i=0;i<tempsjnamejson.length;i++){
                        var temp = {
                            value: tempsjnamejson[i].Name,
                            label: tempsjnamejson[i].Name
                        };
                        sjnamejson.push(temp)
                    }
                    self.SjNameList = sjnamejson;
                },
                error: function() {},
            });
        },
        ok () {
        },
        cancel () {
        },
        querytable(pageNum,pageSize){
            var CarNo = this.CarNo;
            var SjName = this.SjName;
            var StartDate = this.StartDate;
            var EndDate = this.EndDate;
            if(StartDate==''||StartDate==null){
                StartDate = '';
            }else{
                var tempStartDate = new Date(StartDate);
                StartDate = tempStartDate.toLocaleDateString();
            }
            if(EndDate==''||EndDate==null){
                EndDate = '';
            }else{
                var tempEndDate = new Date(EndDate);
                EndDate = tempEndDate.toLocaleDateString();
            }
            var info ;
            var total;
            var jsondata={"page":pageNum, "row":pageSize,"CarNo":CarNo,"SjName":SjName,"StartDate":StartDate,"EndDate":EndDate};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllFanDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data1 = info;
                this.dataCount = total;
            }
        },
        EditFan(FanStartKilo,FanEndKilo,FanKilometers,DocNo,FanRemark,CarNo){
            var self = this;
            if(!self.isInteger(FanEndKilo)){
                toastr.warning("车辆行驶后公里数必须为正整数");
                return false;
            }
            if(FanEndKilo<FanStartKilo){
                toastr.warning("车辆行驶后公里数必须比车辆行驶前公里数大");
                return false;
            }
            var jsondata = {
                "FanStartKilo":FanStartKilo,
                "FanEndKilo":FanEndKilo,
                "FanKilometers":FanKilometers,
                "DocNo":DocNo,
                "FanRemark":FanRemark,
                "CarNo":CarNo
            };
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/EditFan", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.Status=="1"){
                        toastr.success("编辑成功");
                        self.querytable();
                    }
                    if(data.Status=="0"){
                        toastr.error("编辑失败");
                    }
                },
                error: function() {},
            });

        },
        // 2 分页
        changepage(index){
            this.querytable(index,pageSize);
        },
        //判断是否为面积
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },
        //判断是否为整数
        isInteger(value) {
            var patrn = /^[0-9]*[1-9][0-9]*$/;
            if (patrn.exec(value) == null || value == "")
            {
                return false
            } else {
                return true
            }
        }
    },
})